//
//  ByDocumentaryListVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/30.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDocumentaryListVC.h"
#import "ByDocumentaryListCell.h"
#import "ByDocumentaryUserVC.h"
#import "ByDocumentaryDetailVC.h"

@interface ByDocumentaryListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *redArray;


@end

@implementation ByDocumentaryListVC

- (NSMutableArray *)redArray{
    if (_redArray == nil) {
        _redArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _redArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"红人榜";
    [self initViews];
    
    [self loadData];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [ByCoreToolAlert showLoadind:@"加载中"];
    [ByDocumentaryApi followOrderRedList:[[ByNetModel alloc] init] success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        SLog(@"红人榜 = %@", result.data);
        [self.redArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.redArray addObject:netModel];
        }
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.redArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByDocumentaryListCell *cell = [ByDocumentaryListCell cellWithTableView:tableView];
    ByNetModel *netModel = [self.redArray objectAtIndex:indexPath.row];
    cell.netModel = netModel;
    cell.betBlock = ^(){
        if (netModel.plan_count.integerValue > 0) {
            ByDocumentaryUserVC *vc = [[ByDocumentaryUserVC alloc] init];
            vc.netModel = netModel;
            [self by_normalPush:vc hasNavi:YES];
        }
        
    };
    
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    if (indexPath.row > 2) {
        cell.topView.hidden = YES;
        cell.bottomView.hidden = YES;
        cell.numberLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
        cell.numberLabel.textColor = [UIColor blackColor];
    }else{
        if (indexPath.row == 1) {
            cell.topView.image = [UIImage imageNamed:@"yajun皇冠2上"];
            cell.bottomView.image = [UIImage imageNamed:@"yajun皇冠2下"];
            cell.numberLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
            cell.numberLabel.textColor = [UIColor colorWithHexString:@"#C6C6C6"];
        }else if (indexPath.row == 2){
            
            cell.topView.image = [UIImage imageNamed:@"jijun皇冠上3"];
            cell.bottomView.image = [UIImage imageNamed:@"jijun皇冠下3"];
            cell.numberLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
            cell.numberLabel.textColor = [UIColor colorWithHexString:@"#C48E22"];
        }else{
            cell.numberLabel.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:18];
            cell.numberLabel.textColor = [UIColor colorWithHexString:@"#F8E71C"];
        }
    }
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.redArray objectAtIndex:indexPath.row];
    
    if (netModel.plan_count.integerValue > 0) {
        ByDocumentaryUserVC *vc = [[ByDocumentaryUserVC alloc] init];
        vc.netModel = netModel;
        [self by_normalPush:vc hasNavi:YES];
    }
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
